package cn.wolfcode.p2p.base.controller;

import cn.wolfcode.p2p.Exception.CustomException;
import cn.wolfcode.p2p.util.JSONResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class CommonExceptionHandler {
    //处理自定义异常
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public JSONResult customException(CustomException e){
        JSONResult jsonResult = new JSONResult();
        jsonResult.setMsg(e.getMessage());
        return jsonResult;
    }

    //处理系统异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public JSONResult exception(Exception e){
        e.printStackTrace();
        JSONResult jsonResult = new JSONResult();
        jsonResult.setMsg("系统被程序员搞坏了,我们正在殴打程序员当中......");
        return jsonResult;
    }
}
